constructive algorithms greedy sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <vector>
#define pb push_back
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define ll  long long int 
#define gt greater<ll>()
#define ff first
#define ss second
#define pi acos(-1)
#define ull unsigned long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define  mod 1000000007;
#define bi_one(n)      __builtin_popcount(n)
#define str(a)          a.begin(),a.end()
#define mem(a,b)        memset(a, b, sizeof(a) )
#define max_ele(a,n)    *max_element(a,a+n)
#define min_ele(a,n)    *min_element(a,a+n)
#define fio()                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define sp(n) fixed << setprecision(n)
#define Erase(s) s.erase(unique(s.begin(), s.end()), s.end())
#define fn0 for (i = 0; i < n; i++)
#define fn1 for(i=1; i<=n; i++)
#define upper(v,val) upper_bound(v.begin(), v.end(), val)-v.begin()
#define lower(v,val) lower_bound(v.begin(), v.end(), val)-v.begin()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
using namespace std;
const int N=2e5+5;
vector<int> g[N];
bool vis[N];
int n,m,ans=0;
vector<ll>v;
map<ll,ll>mp;
void print(vector<ll> &v)
{
    for (ll i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

void solution()
{
    ll n, m,sq, l = 0,r=0, k = 0, i, j, a=0, b=0, c = 0, d = 0, d1 = 0, sum = 0;
    string s,s1="";
    cin>>n>>m>>k;
    vector<vector<ll>>v;
    fn0
    {
        vector<ll>vv;
        for(j=0; j<m; j++)
        {
            cin>>a;
            vv.pb(a);
        }
        sort(all(vv));
        vector<ll>pp;
        sum=0;
        for(j=0; j<m; j++)
        {
            sum+=vv[j];
            pp.pb(sum);
        }
        v.pb(pp);
    }
    
    vector<pair<ll,ll>> dd;
    for(i=0; i<v.size(); i++)
    {
        c=0;
        sum=0;
        for(j=0; j<v[i].size(); j++)
        {
            if(v[i][j]<=k)
            {
                c++;
                sum+=v[i][j];
            }
            else
                break;

        }
        //cout<<c<< " "<<sum<<endl;
        dd.pb({c,sum});
    }

    d=dd[0].ff;
    d1=dd[0].ss;
    c=1;
    for(i=1; i<dd.size(); i++)
    {
        if(dd[i].ff>d)
            c++;
        else if(dd[i].ff==d)
        {
            if(dd[i].ss<d1)
                c++;
        }
    }
    cout<<c<<endl;
}

int main()
{
    fio();
    int tt = 1;
    cin >> tt;
   // int x=1;
    while (tt--)
    {
        //cout<<"Case "<<x<< ": ";
        solution();
        //x++;
    }
}


Comments

Submit
1 Comments
  • 5/10/2023 21:41 - Africa/Cairo

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define el "\n"
const ll N = 1e5 + 1, INF = 1e18, m = 1e9 + 7, OO = LONG_MAX;
vector <int>vec;
vector<pair<int, int>>pa;
signed main() {
    int t = 1; cin >> t;
    while (t--) {
        int n, m, h;
        cin >> n >> m >> h;
        ll  all = 0, c = 0;
        int ri=0, le=0;
        for (int i = 0; i < n; i++) {
            for (int ii = 0; ii < m; ii++) {
                int a; cin >> a;
                vec.push_back(a);
            }
            sort(vec.begin(), vec.end());
            all = 0, c = 0;
            for (int k = 1; k < m; k++) {
                vec[k] += vec[k - 1];
            }
            for (auto a : vec) {
                if ( a <= h) {
                    c--;
                    all += a;
                }
                else break;
            }
            if (i == 0) {
                le = c;
                ri = all;
            }
            pa.push_back({ c,all });
            vec.clear();
        }
        sort(pa.begin(), pa.end());
        int ans = 0;
        for (auto a : pa) {
            ans++;
            if (a.first == le && a.second == ri) {
                cout << ans << "\n";
                break;
            }
        }
        pa.clear();
    }
    return 0;
}


More Questions

1196C - Robot Breakout
373A - Collecting Beats is Fun
965A - Paper Airplanes
863E - Turn Off The TV
630E - A rectangle
1104A - Splitting into digits
19C - Deletion of Repeats
1550B - Maximum Cost Deletion
1693A - Directional Increase
735D - Taxes
989A - A Blend of Springtime
339C - Xenia and Weights
608A - Saitama Destroys Hotel
1342C - Yet Another Counting Problem
548A - Mike and Fax
109A - Lucky Sum of Digits
864C - Bus
626B - Cards
1221A - 2048 Game
1374D - Zero Remainder Array
1567C - Carrying Conundrum
1029C - Maximal Intersection
922C - Cave Painting
811C - Vladik and Memorable Trip
1589C - Two Arrays
1510K - King's Task
126B - Password
462A - Appleman and Easy Task
839C - Journey
622A - Infinite Sequence